package com.rock.upms.web.common.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 功能： spring容器初始化完成事件 说明：
 * 
 * @author zhousq
 * @version 创建时间：2017-11-01 zhousq2017-11-07：修改onApplicationEvent方法
 */
public class ApplicationContextListener implements ApplicationListener<ContextRefreshedEvent> {

	private static Logger _log = LoggerFactory.getLogger(ApplicationContextListener.class);

	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		// if(contextRefreshedEvent.getApplicationContext().getDisplayName().equals("Root
		// WebApplicationContext")){
		if (null == contextRefreshedEvent.getApplicationContext().getParent()) {
			_log.debug(">>>>> spring初始化完毕 什么都没做 <<<<<");
			// spring初始化完毕后，通过反射调用所有使用RockBaseDao注解的initMapper方法
			// Map<String, Object> beansWithNotAutowired2ServiceMap =
			// this.applicationContext.getBeansWithAnnotation(com.lvbey.service.annotation.NotAutowired2Service.class);
			// ApplicationContext ac =
			// contextRefreshedEvent.getApplicationContext();
			// Map<String, Object> baseServices =
			// ac.getBeansWithAnnotation(ANNBaseService.class);
			// for (Object service : baseServices.values()) {
			// _log.debug(">>>>> {}.initMapper()",
			// service.getClass().getName());
			// try {
			// Method initMapper = service.getClass().getMethod("initMapper");
			// initMapper.invoke(service);
			// } catch (Exception e) {
			// _log.error("初始化BaseService的initMapper方法异常", e);
			// e.printStackTrace();
			// }
			// }
			// 初始化后运行主键初始化方法
			// primaryService.initPrimaryKey();
			// 系统入口初始化
			// Map<String, BaseInterface> baseInterfaceBeans =
			// contextRefreshedEvent.getApplicationContext().getBeansOfType(BaseInterface.class);
			// for(Object service : baseInterfaceBeans.values()) {
			// System.out.println("===zhousq:ApplicationContextListener========系统入口初始化====");
			// _log.debug(">>>>> {}.init()", service.getClass().getName());
			// try {
			// Method init = service.getClass().getMethod("init");
			// init.invoke(service);
			// } catch (Exception e) {
			// _log.error("初始化BaseInterface的init方法异常", e);
			// e.printStackTrace();
			// }
			// }
		} else {
			_log.debug("===zhousq:ApplicationContextListener======contextRefreshedEvent.getApplicationContext().getParent()=null ");
		}
	}
}
